package com.hb.unic.core.base;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonInclude;

import lombok.Data;

/**
 * 公用返回数据模型
 *
 * @since v1.0，2023/8/25 22:44，create by Mr.Huang.
 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@Data
public class Result<T> implements Serializable {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 4226532525009161450L;

    /**
     * 返回码
     */
    private String code = "200";

    /**
     * 返回信息
     */
    private String msg = "success";

    /**
     * 业务数据
     */
    private T data;

    /**
     * 成功
     *
     * @return 结果
     */
    public static <T> Result<T> success() {
        return success(null);
    }

    /**
     * 成功
     *
     * @param data 数据
     * @return 结果
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setData(data);
        return result;
    }

    /**
     * 失败
     *
     * @param codeMessageProvider code-message提供者
     * @return 结果
     */
    public static <T> Result<T> fail(ICodeMessageProvider<String> codeMessageProvider) {
        Result<T> result = new Result<>();
        result.setCode(codeMessageProvider.getCode());
        result.setMsg(codeMessageProvider.getMessage());
        return result;
    }

}
